﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Views;
using System.Collections;
using CPPEI.Coolzon.UI.Event;
using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.UI.Data;
using CPPEI.Coolzon.Event.Core;

namespace CPPEI.Coolzon.Theme.Default.MainViewControl
{
    /// <summary>
    /// SceneNavigation.xaml 的交互逻辑
    /// </summary>
    public partial class SceneNavigation : UserControl
    {
        public Label CurrentLable;
        private List<CoreOpenSceneCompletedEvent> listOpenSceneCompletedEvent;
        private int clicks;
        private string sceneName;
        public SceneNavigation()
        {
            InitializeComponent();
            BindScene();
            lblResetScene.Style = (Style)spNav.Resources["lblEnter"];
            lblResetSolution.Style = (Style)spNav.Resources["lblEnter"];
        }

        public void BindScene()
        {
            if (UIContext.Current.Solution.Scenes == null || !UIContext.Current.Solution.Scenes.Any()) return;
            int i = 0;
            foreach (Scene scene in UIContext.Current.Solution.Scenes)
            {
                if (scene.IsShowInNavigation)
                {
                    i++;
                    Label lbl = new Label();
                    lbl.Name = scene.Name;
                    lbl.Content = scene.Caption;
                    lbl.Tag = i;
                    lbl.Width = 120;
                    lbl.MouseLeftButtonDown += lbl_Click;
                    lbl.DataContext = scene;
                    lbl.Style = (Style)spNav.Resources["lblBase"];
                    spNav.Children.Add(lbl);
                }
            }
        }

        private string LastSelectedName = string.Empty;
        void lbl_Click(object sender, RoutedEventArgs e)
        {
            Label lbl = sender as Label;
            if (lbl == CurrentLable)
            {
                return;

            }
            Scene scene = lbl.DataContext as Scene;
            OpenSceneArgs openSceneArgs = new OpenSceneArgs();
            openSceneArgs.ParametersString.Add("sys:isresetviewstate", "true");

            NCLEventBus.PublishAsync(new NCLOpenSceneEvent(SwitchMode.Replace, "", scene.Name, openSceneArgs));
        }

        void tb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Scene scene = (sender as TextBlock).DataContext as Scene;

            NCLEventBus.PublishAsync(new NCLOpenSceneEvent(SwitchMode.Replace, "", scene.Name, null));
        }

        [Obsolete("已过期，换成OnOpened，OnClosed")]
        public void OnSwitch(SwitchMode mode, string frameName, string type, string name, object data)
        {
            foreach (UIElement label in spNav.Children)
            {
                if (label is Label)
                {
                    Label lbl = label as Label;
                    if (lbl == CurrentLable) break;
                    if (lbl.DataContext is Scene)
                    {
                        Scene scene = lbl.DataContext as Scene;
                        if (scene.Name.Equals(name))
                        {
                            lbl.Style = (Style)spNav.Resources["lblPress"];
                            if (CurrentLable != null)
                            {
                                CurrentLable.Style = (Style)spNav.Resources["lblBase"];
                            }
                            CurrentLable = lbl;
                            break;
                        }
                    }
                }
            }
        }

        private void preNav_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset - 159);
        }

        private void nextNav_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + 159);
        }

        [Common.Subscribe]
        public void OnOpened(CoreOpenSceneCompletedEvent @event)
        {
            if (@event != null )
            {
                sceneName = @event.SceneName;
                if (spNav.Children != null && @event.SwitchMode.Equals(SwitchMode.Replace))
                {
                    foreach (Label lbl in spNav.Children)
                    {
                        if (lbl.Name.Equals(@event.SceneName))
                        {
                            lbl.Style = (Style)spNav.Resources["lblPress"];

                            CurrentLable = lbl;
                        }
                        else
                        {
                            lbl.Style = (Style)spNav.Resources["lblBase"];
                        }
                    }
                }
                else
                {
                    listOpenSceneCompletedEvent = new List<CoreOpenSceneCompletedEvent>();
                    listOpenSceneCompletedEvent.Add(@event);
                }
                //如果是弹出场景则所有按钮不可用
                if (@event.SwitchMode == SwitchMode.Pop)
                {
                    this.IsEnabled = false;
                }
                else
                {
                    this.IsEnabled = true;
                }
            }
        }

        [Subscribe]
        public void OnSceneClosed(CoreCloseSceneCompletedEvent @event)
        {
        }

        private void lblResetScene_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if(!string.IsNullOrEmpty(sceneName))
            {
               Common.EventBus.Instance.Publish(new UI.Views.Events.BeginResetSceneEvent(sceneName));
               //lblResetScene.Style = (Style)spNav.Resources["lblPress"];
            }

        }

        private void lblResetSolution_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Common.EventBus.Instance.Publish(new UI.Views.Events.BeginResetSolutionEvent(UIContext.Current.SolutionName));
           // lblResetSolution.Style = (Style)spNav.Resources["lblPress"];
        }

        [Subscribe]
        public void OnEndResetSceneEvent(UI.Views.Events.EndResetSceneEvent @event)
        {
           
        }

        [Subscribe]
        public void OnEndResetSoltionEvent(UI.Views.Events.EndResetSolutionEvent @event)
        {
            
        }
    }
}
